#!/bin/sh /etc/rc.common
# oscam  Script VER 0.10 for openwrt  by zjhzzyf

START=94

. /etc/functions.sh
path="/usr/oscam/"


kill_oscam()
{
local otherpids
local execute
otherpids=$(ps -a 2>&1 | grep "/usr/bin/oscam" | grep -v $$ | awk -F " " '{print $1}')
echo "$otherpids" | while read execute
do
kill -9 ${execute} 
done
}



oscam_start()
{

 /usr/bin/oscam -b -c $path
  echo "readdy go....."

}



start(){
echo "game  start....."
 enable=$(uci get oscam.@setting[0].enable)
 if [  "$enable" == ""  ];then
 enable=0
 fi

 [  "$enable" == "1"  ]&&oscam_start 

interval=$(uci get oscam.@setting[0].interval)
oscam_cron=`cat /etc/crontabs/root | grep "/etc/init.d/oscam restart"`
if  [ "$enable" == "1" -a "$interval" != "0" ]; then 
	oscam_new="*/${interval} * * * * /etc/init.d/oscam restart"
	if [ "z${oscam_cron}" != "z${oscam_new}" ] ; then
		cat /etc/crontabs/root | grep -v "/etc/init.d/oscam restart" > /tmp/crontabs_oscam
		echo "" >> /tmp/crontabs_oscam
		echo "*/${interval} * * * * /etc/init.d/oscam restart" >> /tmp/crontabs_oscam
		cat /tmp/crontabs_oscam | grep -v ^$ > /etc/crontabs/root
		/etc/init.d/cron restart
	fi
else
	if [ "z${oscam_cron}" != "z" ] ; then
		cat /etc/crontabs/root | grep -v "/etc/init.d/oscam restart" > /tmp/crontabs_oscam
		cat /tmp/crontabs_oscam | grep -v ^$ > /etc/crontabs/root
		/etc/init.d/cron restart
	fi
	
fi
rm -f /tmp/crontabs_oscam

}

stop() {
  kill_oscam
	echo "oscam stopped"
}


